﻿using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.Commons.DataFilters;

namespace AB.ServiceDirectory.Design.Org
{
    public partial class AOrganizationList : Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // LoadAllOrganisation();
                var filter = new AOrganizationFilter();
                ViewState["filter"] = filter;
            }
        }

        /// <summary>
        /// Handles the Command event of the FilterByLetters control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void FilterByLetters_Command(object sender, CommandEventArgs e)
        {
            var filter = ViewState["filter"] as AOrganizationFilter;
            if (filter != null)
            {
                var beginningLetters = StringTools.GetLetterGroup(e.CommandName);
                filter.SetBeginningLetters(beginningLetters);
                GridViewOrg.PageIndex = 0;
                GridViewOrg.DataBind();
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBoxInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxInactive_CheckedChanged(object sender, EventArgs e)
        {
            var filter = ViewState["filter"] as AOrganizationFilter;
            if (filter != null)
            {
                filter.IsActive = CheckBoxInactive.Checked;
                GridViewOrg.DataBind();
            }
        }

        /// <summary>
        /// Handles the Selecting event of the OrganizationObjectDataSource control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.ObjectDataSourceMethodEventArgs"/> instance containing the event data.</param>
        protected void OrganizationObjectDataSource_Selecting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            var filter = ViewState["filter"] as AOrganizationFilter;
            e.InputParameters["filter"] = filter;
        }

        /// <summary>
        /// Handles the RowDataBound event of the GridViewOrg control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void GridViewOrg_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0)
            {
                return;
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
                e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            }
        }

        /// <summary>
        /// Handles the Command event of the LinkButtonOrganizationName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void LinkButtonOrganizationName_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandArgument.Equals("True"))
            {
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Organisation/OrganisationDetails02.aspx?id={0}", e.CommandName));
            }
            else
            {
                BO.AOrganizationBO.UpdateStatus(int.Parse(e.CommandName, CultureInfo.CurrentCulture), true);
                Response.Redirect(string.Format(CultureInfo.CurrentCulture, "~/Organisation/OrganisationDetails02.aspx?id={0}", e.CommandName));
            }
        }

        /// <summary>
        /// Handles the RowCreated event of the GridViewOrg control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void GridViewOrg_RowCreated(object sender, GridViewRowEventArgs e)
        {
            var organisation = e.Row.DataItem as Entities.Organization;
            if (organisation != null)
            {
                if (!organisation.IsActive)
                {
                    e.Row.Attributes["onclick"] = "return Confirm_Edit();";
                }
            }
        }
    }
}